<?php
/*************************************************

推手接口 - the
Author: Wanghubin <17-11-7>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class RecommendController extends Controller {

    //推手申请(未实名)
    public function apply_pusher() {

            $is_pass=0;
            $userid = I('post.userid');
            $realname = I('post.name');
            $idcard = I('post.idcard');
            $phone = I('post.phone');
            $where['id'] = $userid;
            $user_info  = D('User')->get_one($where);
            if($user_info['card_status']==1){
                    $is_pass=1;
            
            }
           // print_r($is_pass);die;
           // echo $realname;die;
            if(!$is_pass){
                $res = $this->realname($realname,$idcard,$userid,1);
            }
            //print_r($res);die;
            if($res['errorCode']==1 or $is_pass==1){
                $map['user_id'] = $userid;
                $res = D('RecommendApply')-> get_one($map);
                if($res){
                    $data = array('errorCode'=>3,'msg'=>'您已申请过了不要重复申请！');
                }else{
                    $arr['name'] = $realname;
                    $arr['tel'] = $phone;
                    $arr['user_id'] = $userid;
                    $arr['status'] =0;
                    $r = D('RecommendApply')->add($arr);
                   if($r){
                        $data = array('errorCode'=>1,'msg'=>'申请提交成功');
                    }else{
                        $data = array('errorCode'=>2,'msg'=>'提交失败请重试！');
                    }
               }
            }else{
                $data = $res;
            }
            $this->ajaxReturn($data);
       
    }
    
        //检查推手是否已经申请过
        public function ajax_push_reg() {
            $user_id = I('post.userid');
            $map['user_id'] = $user_id;
            $recom_apply = D('RecommendApply')->get_one($map);
            if($recom_apply) {
                $data = array('errorCode'=>2,'msg'=>"您已申请过了,请等待审核！");
            } else {
                $data = array('errorCode'=>1,'msg'=>"未申请可以进行投顾申请！");
            }
            $this->ajaxReturn($data);
        }



    //实名
    public function realname($realname,$idcard,$user_id){
        //echo 1 ;die;
        $map['idcard'] = $idcard;
        $idcards = D('User')->get_one($map);
        if($idcards) {
            $data = array(
                    'errorCode' => 8,
                    'msg' => "身份证已被绑定"
                );
                if($val){
                 return $data;
                }else{
                 $this->ajaxReturn($data);
                }
        }


         if(empty($realname)) {
            $data = array(
                'errorCode' => 7,
                'msg' => "名字不能为空"
            );
            if($val){
                 return $data;
            }else{
                 $this->ajaxReturn($data);
            }
        }
        if(empty($idcard)) {
            $data = array(
                'errorCode' => 9,
                'msg' => "身份证号码不能为空"
            );
            if($val){
                 return $data;
            }else{
                 $this->ajaxReturn($data);
            }
        }
        $map['user_id'] = $user_id;
        $re = M('Realname')->where($map)->find();
        if($re){
                $add_day = date('ymd',$re['add_time']);
                $num = $re['num'];
                $today = date('ymd',time());
                    if($num>=5){
                        if($today==$add_day){
                            $data = array(
                                'errorCode' => 4,
                                'msg' => "今天验证次数已经超过5次！明天再来！"
                            );
                        }else{
                            //验证
                            $data =$this->get_real($idcard,$realname,$user_id);
                            if($data['errorCode']!=3){
                                $arr_data = array('num'=>1,'add_time'=>time());
                                $map['id'] = $re['id'];
                                $r = M('Realname')->where($re['id'])->save($arr_data);
                            }
                            
                        }
                    }else{
                            $num = $num+1;
                            $number = 5-$num;
                            $data =$this->get_real($idcard,$realname,$user_id,$number);
                            if($data['errorCode']!=3){
                                $arr_data = array('num'=>$num,'add_time'=>time());
                                $map['id'] = $re['id'];
                                $r = M('Realname')->where($re['id'])->save($arr_data);
                            }
                            
                    }

        }else{
                        $data = $this->get_real($idcard,$realname,$user_id);
                        if($data['errorCode']!=3){
                            $arr_data = array('num'=>1,'add_time'=>time(),'user_id'=>$_SESSION['user']['id']);
                            $r = M('Realname')->data($arr_data)->add();
                        }
        }
        
        return $data;
      

    }



    public function get_real($idcard,$realname,$userid,$number='4'){
        $real =R('Real/v_real',array($idcard,$realname));
       // $real =Array ( 'reason' => '成功', 'result' => Array ( 'realname' => '王华斌', 'idcard'=>'362427199205230819', 'res'=> 1 ), 'error_code'=> 0 );
      
        if($real){
            if($real['error_code']=='0') {
               if($real['result']['res']==1){
                    $data = array(
                        'errorCode' => 1,
                        'msg' => "验证成功！"
                    );
                    //写入用户数据
                    $id = $userid;
                    $map['id'] = $id;
                    $arr_data = array(
                        'realname' => $realname,
                        'idcard'   => $idcard,
                        'card_status' => 1
                    );
                    $user =D('User')->edit($map,$arr_data);
                   // echo D('User')->_sql();die;
                    //實名成功發送消息
                //    R('Msg/sendmail',array($typename='realname',$userid = $id));
                //    $_SESSION['user']['realname'] = $realname;
                //    $_SESSION['user']['idcard'] = $idcard;
                //    $_SESSION['user']['card_status'] = 1;
               }else{
                     $data = array(
                        'errorCode' => 2,
                        'msg' => "验证失败，还剩".$number."次机会"
                    );
               }
            } else {
                $data = array(
                    'errorCode' => 3,
                    'msg' =>$real['reason']
                );
            }
        }else{
                    $data = array(
                        'errorCode' => 3,
                        'msg' => "验证请求失败！"
                    );
        }
        return($data);


    }



     //推手申请平台合作
    public function apply_platfrom(){
        $userid = I('post.userid');
        $platform_id = I('post.platform_id');
        $user_id = $userid;
        $platform_id =$platform_id;
        $map['user_id'] = $user_id;
        $recommend_info = D('Recommend')->get_one($map);
        //print_r($recommend_info);
        $recommend_id = $recommend_info['id'];
        $data['recommend_id'] = $recommend_id;
        $data['platform_id'] = $platform_id;
        $data['status'] = 1;
        $data['add_time'] =time();
        $r = D('RecommendPlatform')->add($data);
            if($r){
                R('Msg/sendmail',array($typename='apply_platfrom',$userid));
                $data = array('errorCode'=>1,'msg'=>"申请成功！");
            }else{
                $data = array('errorCode'=>2,'msg'=>"申请失败！");
            }
        $this->ajaxReturn($data);
    }




    /**
     * [recommend_fllow description] 关注投顾
     * @return [type]               [description]
     */
    public function recommend_fllow(){
        $userid = I('post.userid');
        $recommend_id = I('post.recommend_id');
        if(!$userid){
            $data = array('errorCode'=>2,'msg'=>"请先登录！");
            $this->ajaxReturn($data);exit;
        }
        $maps['id'] = $userid;
        $user =D('User')->get_one($maps);
        if($user['role']==2) {
         $data = array('errorCode'=>3,'msg'=>"投顾无法拜师！");
         $this->ajaxReturn($data);exit;
        } else {
            $map['id'] = $recommend_id;
            $recom = D('Recommend')->get_one($map);
            if($recom) {
                if($user['referral']>0) {
                    $data = array('err'=>4,'msg'=>"已拜过师！");
                    $this->ajaxReturn($data);
                } else {
                    $postdata['referral'] = $recom['user_id'];
                    $map = array();
                    $map['id'] = $userid;
                    $res = D('User')->edit($map,$postdata);
                //    echo D('User')->_sql();die;
                    if($res) {
                        $map = array();
                        $map['id'] = $recom['user_id'];
                        M('User')->where($map)->setInc('score',50);
                        $user_recom = D('User')->get_one($map,$field="id,nickname");
                        $user_recom['recommend_id'] = $recom['id'];
                        $map = array();
                        $map['id'] = $userid;
                        M('User')->where($map)->setInc('score',50);
                        $user_info = D('User')->get_one($map,$field="id,score");
                        $u = array('user_recom'=>$user_recom,'user_info'=>$user_info);
                        $data = array('errorCode'=>1,'msg'=>"拜师成功",'u'=>$u);
                    } else {
                        $data = array('errorCode'=>0,'msg'=>"拜师失败");
                    }
                }
            }
            $this->ajaxReturn($data);
        }

    }


    //申请投顾时创建门派
    public function matial() {
        $user = I('post.userid');
        if(!$user){
            header("location:/Home/User/login");die;
        }
        if(IS_POST) {
            $postdata['user_id'] = $user;
            $postdata['tel'] = I('post.tel');
            $postdata['name'] = I('post.name');
            $postdata['platform_num'] = I('post.platform_num');
            $postdata['year'] = I('post.year');
            $postdata['pit_num'] = I('post.pit_num');
            $postdata['introduce'] = I('post.introduce');
            $postdata['status'] = 0;
            $data = D('RecommendApply')->add($postdata);
            $postdata = array();
            $postdata['user_id'] = $user;
            $postdata['factions'] = I('post.factions');
            $postdata['describe'] = I('post.describe');
            $faction = M('Factions')->data($postdata)->add();
            $postdata = array();
            $postdata['user_id'] = $user;
            $postdata['skill'] = I('post.skill');
            $skill = M('Skill')->data($postdata)->add();
            if($data && $faction && $skill) {
                $res = array('errorCode'=>1,'msg'=>"提交成功！",'data'=>$data,'faction'=>$faction,'skill'=>$skill);
            } else {
                $res = array('errorCode'=>0,'msg'=>"提交失败！");
            }
            $this->ajaxReturn($res);
        }
    }





}